<?php

namespace App\Http\Controllers\User;

use App\Models\Operation;
use App\Models\ProgressList;

use Request;
use App\Http\Controllers\Controller;

class OperateController extends Controller
{
    public function  getProjectSchedule(){

        $request=Request::all();
        $second_type_id=!empty($s_id)?$s_id:$request['s_id'];
        $pid=!empty($pid)?$pid:$request['pid'];
        $p_id=!empty($p_id)?$p_id:$request['p_id'];


        $where=[
            "product_id"=>$pid,
            'process_id'=>$p_id
        ];

        $data=[];
        try{

            $ProgressListModel =new ProgressList();

            $where_progress_list=[
                "second_type_id"=>$second_type_id,
                "show_slider"=>1
            ];

            $progress_list=$ProgressListModel->where($where_progress_list)
                ->orderBy("id")
                ->get();

            $Operate=new Operation();
            $operate=$Operate->where($where)
                ->orderBy("id","desc")
                ->pluck('progress_list_id');
            $new_operate=$Operate->where($where)
                ->orderBy("id","desc")
                ->first();
            $ope=collect($operate)->toArray();
           if(count($ope)){
               foreach ($progress_list as $k => $v){
                   $data[$k]["state"]=0;
                   $data[$k]["time"]="未开始 ";
                   if(in_array($v['id'],$ope)){
                        $data[$k]["state"]=1;
                        $data[$k]["time"]=date("Y-m-d H:i",strtotime($v['created_at']));
                   }
                   $data[$k]["operate"]=$v['name'];

               }
           }else{
               foreach ($progress_list as $k => $v){
                   $data[$k]["state"]=0;
                   $data[$k]["operate"]=$v['name'];
                   $data[$k]["time"]="未开始 ";
                //   $data[$k]["time"]=date("Y-m-d H:i",strtotime($v['created_at']));
               }
           }
        }catch (\Exception $exception){


        }


        return $data;



    }




    public function getProgressList(){




        $request=Request::all();
        $data=[
            'row'=>[],
            'column'=>[],
        ];

        $second_type_id=$request['s_id'];
        $product_id=$request['pid'];
        $process_id=$request['p_id'];
//
//
//        $second_type_id=2;
//        $product_id=154;
//        $process_id=101;

        $where=[
            "product_id"=>$product_id,
            'process_id'=> $process_id
        ];

        $data['row']=$this->getProjectSchedule($second_type_id,$product_id,$process_id);


        try{
            $Operate=new Operation();
            $operate=$Operate->where($where)
                ->orderBy("id","desc")
                ->get();

            $operateList=collect($operate)->toArray();

            if(count($operateList)){

                foreach ($operateList as $key => $value){
                    $data[ "column" ][ $key ]["state"]=0;
                    if($key ==0){
                        $data[ "column" ][ $key ]["state"]=1;
                    }
                    $data[ "column" ][ $key ][ "operate" ] = $value[ 'content' ];
                    $data[ "column" ][ $key ][ "time" ]    = $value[ 'created_at' ];
                }

            }

        }catch (\Exception $e){

        }

          return json_encode($data);


    }



}
